﻿// Common methods

function PostServerData(postUrl, postData, callback) {
    var d = new Date();
    if (!postData)
        postData = { };
    postData.currenttime = d;
    if (typeof contentType == 'undefined')
        contentType = "application/x-www-form-urlencoded";

    $.ajax({
            url: postUrl,
            type: 'post',
            contentType: contentType,
            data: postData,
            success: callback,
            error: function(jqXHR, textStatus, errorThrown) {
                var errorWindow = window.open('', "Error");
                errorWindow.document.write(jqXHR.responseText);
                errorWindow.focus();
            }
        });
}

function PostServerJson(postUrl, postData, callback) {
    var d = new Date();
    if (!postData)
        postData = {};
    postData.currenttime = d;
    if (typeof contentType == 'undefined')
        contentType = "json";

    $.ajax({
        url: postUrl,
        type: 'post',
        contentType: contentType,
        data: postData,
        success: callback,
        error: function (jqXHR, textStatus, errorThrown) {
            var errorWindow = window.open('', "Error");
            errorWindow.document.write(jqXHR.responseText);
            errorWindow.focus();
        }
    });
}

function GetServerData(getUrl, callback) {
    $.ajax({
        url: getUrl,
        type: 'get',
        success: callback,
        error: function (jqXHR, textStatus, errorThrown) {
            var errorWindow = window.open('', "Error");
            errorWindow.document.write(jqXHR.responseText);
            errorWindow.focus();
        }
    });
}
/*
function GetServerData(getUrl, data, callback) {
    var d = new Date();
    if (!data)
        data = {};
    data.currenttime = d;

    $.ajax({
        url: getUrl,
        type: 'get',
        data : data,
        success: callback,
        error: function (jqXHR, textStatus, errorThrown) {
            var errorWindow = window.open('', "Error");
            errorWindow.document.write(jqXHR.responseText);
            errorWindow.focus();
        }
    });
}
*/
function validateMatchPair(input1, input2, message) {
    if (input1.value != input2.value) {
        input2.setAttribute("x-moz-errormessage", message);
        input2.setCustomValidity(message);
    } else {
        input2.setCustomValidity('');
    }
}

function showMessageBox(message, caption) {
    var dialogId = '#popup-messagebox';
    if($(dialogId).length == 0) {
        $('body').append("<div id=\"popup-messagebox\" title=\"" + caption + "\"><p>" + message + "<p/></div>");
    }
    $(dialogId).dialog({
            autoOpen: true,
            width: 400,
            draggable: false,
            resizable: false,
            modal: true,
            buttons: [{ text: "Đóng", click: function() { $(this).dialog("close"); $(dialogId).remove(); } }]
        });
}